package beauty.other;

import java.util.Arrays;

/**
 * 基数排序
 * Created by Robo on 2018/2/5.
 */
public class BaseSort {
    public static void main(String[] args) {
        int[] arr = {2, 4, 1, 5, 7, 6, 9, 0, 3, 8};
        swap(arr);
        System.out.println(hasRepeatNumber(arr));

    }

    public static void swap(int[] arr) {
        for (int b = 0; b < arr.length; b++) {
            while (b != arr[b]) {
                int temp = arr[b];
                if (temp == arr[temp]) {
                    System.out.println("恭喜发财,大吉大利:" + temp);
                    return;
                }
                arr[b] = arr[temp];
                arr[temp] = temp;
            }
        }
        System.out.println("今晚吃鸡: " + Arrays.toString(arr));
//        swap(arr, arr[b]);
    }

    public static int hasRepeatNumber(int[] data) {
        int n = data.length;
        for (int i = 0; i < n; ++i) {
            while (data[i] != i) {
                int tmp = data[i];
                if (data[tmp] == tmp) {
                    return tmp;
                }

                // swap
                data[i] = data[tmp];
                data[tmp] = tmp;
            }
        }
        return -1;
    }
}
